package com.jie.service.impl;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jie.entity.Admin;
import com.jie.mapper.AdminMapper;
import com.jie.service.AdminService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jie.utility.QueryInfo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jie
 * @since 2023-12-05
 */
@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements AdminService {
    @Autowired
    private AdminMapper adminMapper;


    public Page selectAllAdmin(QueryInfo queryInfo,Admin admin) {

        Page page = new Page(queryInfo.getPagenum(), queryInfo.getPagesize());
        LambdaQueryWrapper<Admin> queryWrapper=new LambdaQueryWrapper<>();
        if(admin.getType()==1){
            page = adminMapper.selectPage(page, null);
        }else {
            queryWrapper.eq(Admin::getCreateAdminId,admin.getAdminId());
            page = adminMapper.selectPage(page, queryWrapper);
        }

        return page;
    }


    public boolean addadmin(Admin admin) {

        int insert = adminMapper.insert(admin);
        if(insert==0){
            return false;
        }
        return true;
    }

    @Override
    public boolean updateAdmin(Admin admin) {
        int update = adminMapper.updateById(admin);
        if(update==0){
            return false;
        }
        return true;
    }

    @Override
    public boolean delAdmin(Admin admin) {
        int delete = adminMapper.deleteById(admin.getAdminId());
        if(delete==0){
            return false;
        }
        return true;

    }


    public Page selectAdmin(QueryInfo queryInfo,Admin admin) {

        Page page = new Page(queryInfo.getPagenum(), queryInfo.getPagesize());
        LambdaQueryWrapper<Admin> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(admin.getUsername()),Admin::getUsername,admin.getUsername());
        page = adminMapper.selectPage(page, queryWrapper);
        return page;
    }
}
